<?php

namespace co2\dev\v0_0_0\bash;

use co2\dev\v0_0_0 as co2;

class tar extends co2\bash

{
	public function
	__construct
	(
		$action='c',
		$format='z',
		$file,
		\stdClass $options=null
	)
	
	{
		$this->cmd='tar ';
		
		switch($action)
		
		{
			case'c':case'create':
			
				$this->cmd.="-{$format}c";
				
				$name="file.tar.";
				
				switch($format)
				
				{
					case'z':	$name.='gz'; break;
				}
				
				if(is_object($options))
				
				{
					foreach(array('verbose','v') as $_)
					if(isset($options->$_) && is_bool($options->$_) && $options->$_===true)
					$this->cmd.="v";
					
					if(isset($options->name) && is_string((string)$options->name))
					$name=$options->name;
				}
				
				$this->cmd.="f $name $file";
				
				$this->exec(new co2\RegEx("/(tar: Removing leading `\/' from member names)?/"));
			
			break;
			
			case'x':case'extract':
			
				$this->cmd.="-{$format}x";
				
				$target=null;
				
				if(is_object($options))
				
				{
					foreach(array('verbose','v') as $_)
					if(isset($options->$_) && is_bool($options->$_) && $options->$_===true)
					$this->cmd.="v";
					
					if(isset($options->target) && is_string((string)$options->target))
					$target=$options->target;
				}
				
				$this->cmd.="f $file";
				
				if(!is_null($target))
				$this->cmd.=" -C $target";

				$this->exec();
			
			break;
		}
		
	}
}
